const path = require('path');
const fs = require('fs');
const Books = require('../model/booksModel');
const { promisify } = require('util');
const renamePromisify = promisify(fs.rename);
const unlinkPromisify = promisify(fs.unlink);

const booksController = {};

// booksController.PORT = 4500;
require('dotenv').config();
let Ip = `http://127.0.0.1:${process.env.PORT}/`;

// 查找
booksController.findAll = async (req, res) => {
  let data = await Books.findAll();
  // console.log(data);
  let dataArr = [];
  data.forEach((item) => {
    item.cover = `${Ip}${item.cover}`;
    dataArr.push(item);
  });
  // res.json(data);
  res.json(dataArr);
};

// 获取单条数据
booksController.findOne = async (req, res) => {
  let { id } = req.params;
  let data = await Books.findOne({ where: { id } });
  data.cover = `${Ip}${data.cover}`;
  res.json(data);
};

// 添加
booksController.addBook = async (req, res) => {
  console.log(req.body);
  console.log(req.file);
  let file = req.file;
  let cover;
  if (file) {
    let { originalname, destination, filename } = file;
    const oldPath = path.join(destination, filename);
    // const oldPath = destination + '/' + filename;
    const suffix = originalname.substring(originalname.lastIndexOf('.'));
    newPath = oldPath + suffix;
    await renamePromisify(oldPath, newPath);
    cover = newPath;
  }
  let result = { ...req.body, cover };
  let data = await Books.create(result);
  //   console.log(data);
  if (data) {
    res.json({ code: 20000, message: '添加成功' });
  } else {
    res.json({ code: 20001, message: '添加失败' });
  }
};

// 编辑
booksController.editBook = async (req, res) => {
  let { id } = req.query;
  console.log(req.body);
  console.log(req.file);
  let file = req.file;
  let cover;
  let result;
  if (file) {
    let { originalname, destination, filename } = file;
    const oldPath = path.join(destination, filename);
    // const oldPath = destination + '/' + filename;
    const suffix = originalname.substring(originalname.lastIndexOf('.'));
    newPath = oldPath + suffix;
    await renamePromisify(oldPath, newPath);
    cover = newPath;
    let { dataValues } = await Books.findOne({ where: { id } });
    dataValues.cover && (await unlinkPromisify(dataValues.cover));
    result = { ...req.body, cover };
  } else {
    result = req.body;
  }

  let data = await Books.update(result, { where: { id } });
  // 根据要编辑的这个ID去找到，他没修改之前的图片的路径，然后删除

  if (data) {
    res.json({ code: 20000, message: '编辑成功' });
  } else {
    res.json({ code: 20001, message: '编辑失败' });
  }
};

// 删除
booksController.delete = async (req, res) => {
  let { id } = req.query;
  // 先找到数据库中存储的路径
  let { dataValues } = await Books.findOne({ where: { id } });
  console.log(dataValues.cover);
  // 再把数据库的那条数据删除
  let data = await Books.destroy({ where: { id } });
  // 然后再把本地的路径删除
  // console.log(__dirname + dataValues.cover);
  dataValues.cover && (await unlinkPromisify(dataValues.cover));
  if (data) {
    res.json({ code: 20000, message: '删除成功' });
  } else {
    res.json({ code: 20001, message: '删除失败' });
  }
};

module.exports = booksController;
